package dj.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dj.dao.BaseDao;
import dj.model.ZhebiaoXishu;
import dj.pageModel.PzhebiaoXishu;
import dj.service.ZhebiaoxishuService;

@Service("zbxsService")
public class ZhebiaoxishuServiceImpl implements ZhebiaoxishuService {

	private BaseDao<ZhebiaoXishu> zbxsDao;

	public BaseDao<ZhebiaoXishu> getZbxsDao() {
		return zbxsDao;
	}

	@Autowired
	public void setZbxsDao(BaseDao<ZhebiaoXishu> zbxsDao) {
		this.zbxsDao = zbxsDao;
	}

	public void save(PzhebiaoXishu px) {
		ZhebiaoXishu zx = new ZhebiaoXishu();
		BeanUtils.copyProperties(px, zx);
		zx.setId(UUID.randomUUID().toString());
		zx.setCreateDate(new Date());
		zx.setStatus(PzhebiaoXishu.SATUS_CLOSE);
		zbxsDao.save(zx);
	}

	public void update(PzhebiaoXishu px) {
		ZhebiaoXishu zx = getZhebiaoxishuByid(px.getId());
		BeanUtils.copyProperties(px, zx, new String[]{"createDate", "status"});
		zbxsDao.update(zx);
	}

	public void updateStatus(PzhebiaoXishu px) {
		zbxsDao.executeHql("update ZhebiaoXishu set status='" + PzhebiaoXishu.SATUS_CLOSE + "'");
		ZhebiaoXishu zx = getZhebiaoxishuByid(px.getId());
		zx.setStatus(PzhebiaoXishu.STATUS_OPEN);
		zbxsDao.update(zx);
	}

	public ZhebiaoXishu getZhebiaoxishuByid(String id) {
		String hql = "from ZhebiaoXishu where id=:id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		return zbxsDao.get(hql, params);
	}

	public ZhebiaoXishu getZhebiaoxishuByStatus(String status) {
		String hql = "from ZhebiaoXishu where status=:status";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", status);
		return zbxsDao.get(hql, params);
	}

}
